package com.et114.modules.system.dictionary.tag;

import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.commons.beanutils.PropertyUtils;

import com.et114.modules.system.dictionary.DictionaryCache;
import com.et114.modules.system.dictionary.vo.BizOption;

/**
 * @author 
 *
 */
public class DictBizOptionNameTag extends TagSupport {
	private static final long	serialVersionUID	= - 2458936183152440052L;
	protected String biztypekey; 
	protected String name = null;
	protected String property = null;
	/**
	 * @retur
	/*
	 * ���� Javadoc��
	 * 
	 * @see javax.servlet.jsp.tagext.Tag#doEndTag()
	 */
	public int doEndTag() throws JspException {
		DictionaryCache cache = DictionaryCache.getInstance();
		Object bean  = pageContext.findAttribute(name);
		String value = null;
		try {
			value =  PropertyUtils.getProperty(bean, property)+"";
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		} catch (InvocationTargetException e1) {
			e1.printStackTrace();
		} catch (NoSuchMethodException e1) {
			e1.printStackTrace();
		}
		StringBuffer results = new StringBuffer();
		List options = new ArrayList();
		try {
			options = cache.listBizOptions(biztypekey,true);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}		
		for (Iterator it = options.iterator(); it.hasNext();) {
			BizOption option = (BizOption) it.next();
			String optionvalue = option.getOptionValue();
			
			if (optionvalue.equals(value)) {
				results.append(option.getOptionName());
			}
		}
		JspWriter writer = pageContext.getOut();
		try {
			writer.write(results.toString());
		} catch (Exception e) {
			throw new JspException(e.getMessage());
		}
		return (EVAL_PAGE);
	}

	/**
	 * @return Returns the biztypekey.
	 */
	public String getBiztypekey() {
		return biztypekey;
	}

	/**
	 * @param biztypekey The biztypekey to set.
	 */
	public void setBiztypekey(String biztypekey) {
		this.biztypekey = biztypekey;
	}

	/**
	 * @return Returns the name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name The name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return Returns the property.
	 */
	public String getProperty() {
		return property;
	}

	/**
	 * @param property The property to set.
	 */
	public void setProperty(String property) {
		this.property = property;
	}

}
